package com.example.demo.controller;

import com.example.demo.pojo.RoomCommodity;
import com.example.demo.service.RoomCommdityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@ComponentScan
@Controller
public class RoomCommodityController {
    @Autowired
    private RoomCommdityService roomCommdityService;
    @Transactional
    @RequestMapping("/admin/getRoomCommAll")

    public String getRoomCommAll(Model model) {
        model.addAttribute("roomCommList", roomCommdityService.getRoomCommAll());
        return "testModel";
    }
    @Transactional
    @RequestMapping("/admin/updateRoomCommodityNameByRoomID")
    @ResponseBody
    public int updateRoomCommodityNameByRoomID(String commName, int roomID) {
        int flag = roomCommdityService.updateRoomCommodityNameByRoomID(commName, roomID);
        return flag;
    }
    @Transactional
    @RequestMapping("/admin/updateRoomCommodityByRoomID")
    @ResponseBody
    public int updateRoomCommodityByRoomID(String commName, int commPrice, int commNum, int roomID) {
        int flag = roomCommdityService.updateRoomCommodityByRoomID(commName, commPrice, commNum, roomID);
        return flag;
    }
    @Transactional
    @RequestMapping("/admin/addRoomCommodity")
    @ResponseBody
    public int addRoomCommodity(String commName, int commPrice, int commNum, int roomID) {
        int flag = roomCommdityService.addRoomCommodity(commName, commPrice, commNum, roomID);
        return flag;
    }

    /**
     * 获取指定房号所有的商品 API(Restful风格)
     *
     * @param roomID
     * @return
     */
    @Transactional
    @GetMapping("/commodities")
    @ResponseBody
    @CrossOrigin
    public List<RoomCommodity> getCommodityList(@RequestParam("roomId") int roomID) {
        return roomCommdityService.getRoomCommByRoomID(roomID);
    }

    /**
     * 增加商品 API (Restful风格)
     * 返回JSON，增加成功就返回{"result":1},失败就返回{"result":-1}
     *
     * @param roomCommodity
     * @return
     */
    @Transactional
    @PostMapping("/commodities")
    @ResponseBody
    @CrossOrigin
    public String addCommodity(@RequestBody RoomCommodity roomCommodity) {
        int res=roomCommdityService.addRoomCommodity(roomCommodity.getCommName(), roomCommodity.getCommPrice(), roomCommodity.getCommNum(), roomCommodity.getRoomID());
        return "{\"result\":"+res+"}";
    }

    /**
     * 删除指定商品 API (Restful风格)
     * 返回JSON，增加成功就返回{"result":1},失败就返回{"result":-1}
     *
     * @param roomID
     * @param commName
     * @return
     */
    @Transactional
    @DeleteMapping("/commodities")
    @ResponseBody
    @CrossOrigin
    public String deleteCommodity(@RequestParam("roomId") int roomID, @RequestParam("commName") String commName) {
        int res=roomCommdityService.deleteCommodityByRoomIDAndCommName(roomID, commName);
        return "{\"result\":"+res+"}";
    }

    /**
     * 返回房间商品管理页面
     *
     * @return
     */
    @RequestMapping("/admin-roomComm")
    public String adminRoomComm(){return "/admin/admin-roomComm";}
}
